
import {CENTER_MENU} from '../layout-config'
import useUserStore from '@/store/user'

/**
 * 获取所有叶子节点
 */
const _findLeafs = (menus: RouteMenuItem[]) => {
  const names:string[] = []
  const leafs:RouteMenuItem[] = []
  const fn = (ms:RouteMenuItem[]) => {
    for (let item of ms) {
      if (!item.children) {
        names.push(item.name as string)
        leafs.push({...item})
      }
      if (item.children) {
        fn(item.children)
      }
    }
  }
  fn(menus)

  return {
    names,
    leafs
  }
}

/**
 * 格式化树形菜单至分组扁平化处理
 */
export const pareseCenterMenuToGroup = () => {
  const userStore = useUserStore()
  const groups = [] as any[]
  const menus = [...CENTER_MENU]
  for (let item of menus) {
    if (item.children) {
      groups.push({
        key: item.key,
        ..._findLeafs(item.children)
      })
    }
  }
  userStore.groupCenterMenus = groups
}